﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdvanceLib.ClassExtensions;
using AdvanceLib.ScriptCore;
using AdvanceLib.ScriptCore.DebugCore;

namespace AdvanceLib.Extension.XseScript.DebugPasses
{
    public class ParameterValidationPass : CommandPass
    {
        #region Constructors
        public ParameterValidationPass()
        {
            this.InvalidParameters = new List<InvalidParameterPass>();
        }
        #endregion

        #region Properties
        public List<InvalidParameterPass> InvalidParameters { get; set; }
        #endregion

        #region Methods
        public bool IsParameterInvalid(int lineIndex, int linePartIndex)
        {
            return this.InvalidParameters.Where(param => param.LineIndex == lineIndex &&
                    param.LinePartIndex == linePartIndex).ToArray().Length > 0;
        }
        #endregion

        #region IterativeCommandDebugger Members
        public override void Pass(DebugResult result, string linePart)
        {
            if (this.CurrentLinePartIndex != 0)
            {
                bool isValid = linePart.IsHex() || linePart.IsDecimal() || linePart.IsPointerName();

                if (!isValid)
                {
                    this.InvalidParameters.Add(new InvalidParameterPass()
                    {
                        LineIndex = this.CurrentLineIndex,
                        LinePartIndex = this.CurrentLinePartIndex
                    });

                    result.Errors.Add(new DebugError()
                    {
                        Line = this.CurrentLineIndex,
                        Message = "Invalid parameter " + this.CurrentLinePartIndex.ToString() +
                                  ": Expecting 0x[hex], [decimal] or @[pointer]"
                    });
                }
            }
        }
        #endregion
    }
}
